
const _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');

exports.__esModule = true;
exports.default = void 0;

const _babelHelperVueJsxMergeProps = _interopRequireDefault(require('@vue/babel-helper-vue-jsx-merge-props'));

const _utils = require('../../utils');

const _functional = require('../../utils/functional');

const _constant = require('../../utils/constant');

// Utils
const _createNamespace = (0, _utils.createNamespace)('sku-header');
const createComponent = _createNamespace[0];
const bem = _createNamespace[1];

function getSkuImg(sku, selectedSku) {
  let img;
  sku.tree.some((item) => {
    const id = selectedSku[item.k_s];

    if (id && item.v) {
      const matchedSku = item.v.filter((skuValue) => skuValue.id === id)[0] || {};
      img = matchedSku.previewImgUrl || matchedSku.imgUrl || matchedSku.img_url;
      return img;
    }

    return false;
  });
  return img;
}

function SkuHeader(h, props, slots, ctx) {
  let _slots$skuHeaderIma;

  const { sku } = props;
  const { goods } = props;
  const { skuEventBus } = props;
  const { selectedSku } = props;
  const goodsImg = getSkuImg(sku, selectedSku) || goods.picture;

  const previewImage = function previewImage() {
    skuEventBus.$emit('sku:previewImage', goodsImg);
  };

  return h('div', (0, _babelHelperVueJsxMergeProps.default)([{
    class: [bem(), _constant.BORDER_BOTTOM],
  }, (0, _functional.inherit)(ctx)]), [h('div', {
    class: bem('img-wrap'),
    on: {
      click: previewImage,
    },
  }, [h('img', {
    attrs: {
      src: goodsImg,
    },
  }), (_slots$skuHeaderIma = slots['sku-header-image-extra']) == null ? void 0 : _slots$skuHeaderIma.call(slots)]), h('div', {
    class: bem('goods-info'),
  }, [slots.default && slots.default()])]);
}

SkuHeader.props = {
  sku: Object,
  goods: Object,
  skuEventBus: Object,
  selectedSku: Object,
};

const _default = createComponent(SkuHeader);

exports.default = _default;
